package cn.itcast.oa.model;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
 * 部门实体类
 * @author 留得残荷听雨声
 *
 */
@Entity
@Table(name="itcast_department")
public class Department implements java.io.Serializable{
    /**
	 * 实现序列化
	 */
	private static final long serialVersionUID = 1L;
	
	private Long id;
    private String name;//部门名称
    private String description;//部门介绍
    private Department parent;//上级部门
    private Set<User> users=new HashSet<User>();//成员
    private Set<Department> children = new HashSet<Department>();//部门成员
    
    @Id
    @GeneratedValue
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	
	@ManyToOne//多对一的在多的一方为主
	@JoinColumn(name="parentId")
	public Department getParent() {
		return parent;
	}
	public void setParent(Department parent) {
		this.parent = parent;
	}
	
	@OneToMany(mappedBy="department")
	public Set<User> getUsers() {
		return users;
	}
	public void setUsers(Set<User> users) {
		this.users = users;
	}
	
    /**
     * cascade=CascadeType.REMOV允许级联删除
     * fetch=FetchType.EAGER先取出房主懒加载
     * @return
     */
	@OneToMany(cascade=CascadeType.REMOVE,mappedBy="parent")
	public Set<Department> getChildren() {
		return children;
	}
	public void setChildren(Set<Department> children) {
		this.children = children;
	}
    
}
